/*
	Flie:						LoadState.cpp
	Course:				SGP 1208
	Author:				Kori Po
	Purpose:				Loading a saved game
*/

#include "Game.h"
#include "MainMenuState.h"

// Temporary Development Includes
#include "GameplayState.h"
#include "TempFont.h"

#include "LoadState.h"
#include "../TinyXML/tinyxml.h"

#include  "Player.h"
#include	"Entity.h"
#include	"Ability.h"

CLoadState::CLoadState(void)
{
	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;

	m_nBackGroundID = -1;
	m_nOptionsBoxID = -1;

	m_nSelectedItem = 0;

	m_pGetIt = new int;

	CGame* TheGame = CGame::GetInstance();

	for(int x = 0; x < LOADMENUITEMS; x++)
	{
		m_MenuSelection[x] = false;

		RECT r = {TheGame->GetScreenWidth()/8,
			(TheGame->GetScreenHeight()/4) + (75 * x), 
			TheGame->GetScreenWidth()/8 + (512*2),
			(TheGame->GetScreenHeight()/4) + (75 * x) + 256 };

		m_MenuRect[x] = r;
		// Exit Box expection
		if(x == 3)
		{
			m_MenuSelection[x] = false;

			RECT r = {TheGame->GetScreenWidth()/8,
			(TheGame->GetScreenHeight()/4) + (75 * x), 
			TheGame->GetScreenWidth()/8 + (225*2),
			(TheGame->GetScreenHeight()/4) + (75 * x) + 43 };

			m_MenuRect[x] = r;
		}
	}

}

CLoadState::~CLoadState(void)
{
	/* Do Nothing */
}

/*static*/ CLoadState* CLoadState::GetInstance( void )
{
	// Static local variable - Stored in GLOBAL memory
	static CLoadState s_instance;
	return &s_instance;
}

void CLoadState::Enter(void)
{
	m_3DGuy				= CSGD_Direct3D::GetInstance();
	m_InputGuy			= CSGD_DirectInput::GetInstance();
	m_TextureGuy		= CSGD_TextureManager::GetInstance();
	m_AudioGuy			= CSGD_XAudio2::GetInstance();

	m_nMenuWizardID	= m_TextureGuy->LoadTexture(_T("Resource/TempMouseCursor.png"), D3DCOLOR_XRGB(255,255,255));
	m_nBackGroundID	= m_TextureGuy->LoadTexture(_T("Resource/TempFusionBackground.png"), D3DCOLOR_XRGB(255,255,255));
	m_nOptionsBoxID		= m_TextureGuy->LoadTexture(_T("Resource/TempOptionItemBox.png"), D3DCOLOR_XRGB(255,255,255));
	m_nTextBoxID				= m_TextureGuy->LoadTexture(_T("Resource/TempTextBox.png"), D3DCOLOR_XRGB(255,255,255));

	m_MenuFont = new CTempFont( );

	GrabSavedGameA();
}

void CLoadState::Exit(void)
{
	if(m_nMenuWizardID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nMenuWizardID);
		m_nMenuWizardID = -1;
	}
	if(m_nOptionsBoxID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nOptionsBoxID);
		m_nOptionsBoxID = -1;
	}

	m_3DGuy				= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;
}

bool CLoadState::Input(void)
{
	if( m_InputGuy->KeyDown( DIK_LMENU ) && m_InputGuy->KeyPressed( DIK_RETURN )|| m_InputGuy->KeyDown( DIK_RMENU ) && m_InputGuy->KeyPressed( DIK_RETURN ) )
	{
			CGame* TheGame	=	CGame::GetInstance();
			TheGame->SetIsWindowed(!TheGame->GetIsWindowed());
			m_3DGuy->ChangeDisplayParam(TheGame->GetScreenWidth(),TheGame->GetScreenHeight(),TheGame->GetIsWindowed());
			return true;
	}

	if( m_InputGuy->KeyPressedEx(DIK_DOWN) || m_InputGuy->JoystickDPadPressed(DIR_DOWN, 0) )
	{
		if(m_nSelectedItem + 1 > 3)
		{
			m_nSelectedItem = 0;
		}
		else
		{
			m_nSelectedItem++;
		}
	}

	if( m_InputGuy->KeyPressedEx(DIK_UP) || m_InputGuy->JoystickDPadPressed(DIR_UP, 0) )
	{
		if(m_nSelectedItem - 1 < 0)
		{
			m_nSelectedItem = 3;
		}
		else
		{
			m_nSelectedItem--;
		}
	}

	if( m_InputGuy->KeyPressedEx(DIK_RETURN ) || m_InputGuy->MouseButtonPressed(0) || m_InputGuy->JoystickButtonPressed( 0, 0) )
	{
		if(m_nSelectedItem == 0)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();
			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 1)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();
			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 2)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();
			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 3)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CMainMenuState* TheMain = CMainMenuState::GetInstance();
			TheGame->ChangeState(TheMain);
			return true;
		}
	}
	
	for(int x = 0; x < 4; x++)
	{
		m_MenuSelection[x] = false;
	}

	if( m_InputGuy->KeyPressedEx(DIK_ESCAPE ) || m_InputGuy->JoystickButtonPressed( 2, 0)  )
	{
		m_nSelectedItem = 3;
		return true;
	}

	m_MenuSelection[m_nSelectedItem] = true;
	return true;
}

void CLoadState::Update(float fElapsedTime)
{
	Mouseinthebox( );
}

void CLoadState::Render(void)
{
	CGame* TheGame = CGame::GetInstance();

	//Draw Background
	RECT ToDraw = {0,0,1920,1080};
	m_TextureGuy->Draw(m_nBackGroundID,0,0,1,1,&ToDraw);
	
	// Saved Games Section
	for( int x  = 0; x < LOADMENUITEMS; x++)
	{
		// Exit Menu Option
		if(x == TEXTBOXITEMS)
		{
			m_TextureGuy->Draw(m_nOptionsBoxID,TheGame->GetScreenWidth()/2 - 100,(TheGame->GetScreenHeight()/4) + (150 * x),1,1);
			if(!m_MenuSelection[x])
			{
				m_MenuFont->Print("EXIT", TheGame->GetScreenWidth()/2 - 50,(TheGame->GetScreenHeight()/4) + (150 * x) + 15, 1, D3DCOLOR_XRGB(255,255,255));
			}
			else
			{
				m_MenuFont->Print("EXIT", TheGame->GetScreenWidth()/2 - 50,(TheGame->GetScreenHeight()/4) + (150 * x) + 15, 1.4, D3DCOLOR_XRGB(255,0,0));
			}
		}
		else
		{
			TCHAR  	bufferA[ 100 ];
			TCHAR  	bufferB[ 100 ];
			TCHAR  	bufferC[ 100 ];
			
			char	strName[100];
			char	strLevel[100];
			char	strWorld[100];

			int	nNumSave = x+1;
			int	nNumLevel = SaveLevelA;
			int	nNumWorld = SaveWorldA;
			
			_stprintf_s( bufferA, 100, _T("GAME SAVE %i"), nNumSave );
			_stprintf_s( bufferB, 100, _T("LEVEL: %i"), nNumLevel );
			_stprintf_s( bufferC, 100, _T("WORLD: %i"), nNumWorld );

			wcstombs_s( nullptr, strName, 100, bufferA, _TRUNCATE );
			wcstombs_s( nullptr, strLevel, 100, bufferB, _TRUNCATE );
			wcstombs_s( nullptr, strWorld, 100, bufferC, _TRUNCATE );

			m_TextureGuy->Draw(m_nTextBoxID,TheGame->GetScreenWidth()/4,(TheGame->GetScreenHeight()/8) + (150 * x),1,.5);
			if(!m_MenuSelection[x])
			{
				m_MenuFont->Print(strName, TheGame->GetScreenWidth()/4 + 20,(TheGame->GetScreenHeight()/8) + (150 * x) + 15, 1, D3DCOLOR_XRGB(255,255,255));
				m_MenuFont->Print(strLevel, TheGame->GetScreenWidth()/4 + 20,(TheGame->GetScreenHeight()/8) + (150 * x) + 45, 1, D3DCOLOR_XRGB(255,255,255));
				m_MenuFont->Print(strWorld, TheGame->GetScreenWidth()/4 + 20,(TheGame->GetScreenHeight()/8) + (150 * x) + 75, 1, D3DCOLOR_XRGB(255,255,255));
			}
			else
			{
				m_MenuFont->Print(strName, TheGame->GetScreenWidth()/4 + 20,(TheGame->GetScreenHeight()/8) + (150 * x) + 15, 1.4, D3DCOLOR_XRGB(255,255,255));
				m_MenuFont->Print(strLevel, TheGame->GetScreenWidth()/4 + 20,(TheGame->GetScreenHeight()/8) + (150 * x) + 45, 1.4, D3DCOLOR_XRGB(255,255,255));
				m_MenuFont->Print(strWorld, TheGame->GetScreenWidth()/4 + 20,(TheGame->GetScreenHeight()/8) + (150 * x) + 75, 1.4, D3DCOLOR_XRGB(255,255,255));
			}
		}
	}

	// Draw Title
	m_MenuFont->Print("LOAD A GAME", TheGame->GetScreenWidth()/2 - 100, 20, 1, D3DCOLOR_XRGB(255,255,255));

	// Draw Mouse
	RECT ToDrawMouse = {0,0,38,48};
	if(m_InputGuy->MouseButtonDown(MOUSE_LEFT))
	{
		RECT ToDrawMouse = {39,0,75,48};
	}

	m_TextureGuy->Draw(m_nMenuWizardID,m_InputGuy->MouseGetPosX(),m_InputGuy->MouseGetPosY(),1,1,&ToDrawMouse);
}

bool CLoadState::GrabSavedGameA()
{
	CGame* TheGame	=	CGame::GetInstance();

	TiXmlDocument TheDoc;

	if(TheDoc.LoadFile("Resource/Files/SaveFileA.xml"))
	{
		TiXmlElement* pRoot = TheDoc.RootElement();

		if(pRoot == nullptr)
			return false;

		// Set Index
		TiXmlElement* pSlot = pRoot->FirstChildElement("Slot");

		int nIndex;
		pSlot->Attribute("SlotNumber", &nIndex);
		m_bHasGame[nIndex-1] = true;

		// Set Player
		TiXmlElement* pPlayer = pRoot->FirstChildElement("Player");

		int nBuffer;
		m_pAGuy = new CPlayer;

		pPlayer->Attribute("Active", &nBuffer);
		if(nBuffer)
		{
			pPlayer->Attribute("HP",	&nBuffer);
			m_pAGuy->SetHP(nBuffer);

			pPlayer->Attribute("MP",	&nBuffer);
			m_pAGuy->SetMP(nBuffer);

			pPlayer->Attribute("HPRegen", &nBuffer);
			m_pAGuy->SetHPRegen(nBuffer);

			pPlayer->Attribute("MPRegen", &nBuffer);
			m_pAGuy->SetMPRegen(nBuffer);

			pPlayer->Attribute("Defense", &nBuffer);
			m_pAGuy->SetDefense(nBuffer);

			pPlayer->Attribute("AtkPower", &nBuffer);
			m_pAGuy->SetAtkPower(nBuffer);

			pPlayer->Attribute("AtkSpeed", &nBuffer);
			m_pAGuy->SetAtkSpeed(nBuffer);

			pPlayer->Attribute("AP", &nBuffer);
			m_pAGuy->SetAP(nBuffer);

			pPlayer->Attribute("CDR", &nBuffer);
			m_pAGuy->SetCDR(nBuffer);

			pPlayer->Attribute("Movement", &nBuffer);
			m_pAGuy->SetMovement(nBuffer);

			pPlayer->Attribute("Item", &nBuffer);

			if(nBuffer)
			{
				CEntity* SomeItem = new CEntity;
				m_pAGuy->SetMyItem(SomeItem);
			}

			pPlayer->Attribute("MaxHP", &nBuffer);
			m_pAGuy->SetMaxHP(nBuffer);

			pPlayer->Attribute("MaxMP", &nBuffer);
			 m_pAGuy->SetMaxMP(nBuffer);

			pPlayer->Attribute("PosX", &nBuffer);
			m_pAGuy->SetX(nBuffer);

			pPlayer->Attribute("PosY", &nBuffer);
			m_pAGuy->SetY(nBuffer);

			pPlayer->Attribute("ObjectType", &nBuffer);
			m_pAGuy->SetType(nBuffer);

			pPlayer->Attribute("Width", &nBuffer);
			m_pAGuy->SetWidth(nBuffer);

			pPlayer->Attribute("Height", &nBuffer);
			m_pAGuy->SetHeight(nBuffer);
		}

		// Set World
		TiXmlElement* pWorld = pRoot->FirstChildElement("World");

		int nWorld;
		pSlot->Attribute("CurrentWorld", &nWorld);
		SaveWorldA = nWorld;

		// Set Level
		TiXmlElement* pLevel = pRoot->FirstChildElement("Level");

		int nLevel;
		pSlot->Attribute("Current1Level", &nLevel);
		SaveLevelA = nLevel;

		*m_pGetIt = 1;
	}
	return true;
}

void CLoadState::Mouseinthebox()
{
	int MouseX = m_InputGuy->MouseGetPosX();
	int MouseY = m_InputGuy->MouseGetPosY();
	
	POINT Clicker;
	Clicker.x = m_InputGuy->MouseGetPosX();
	Clicker.y = m_InputGuy->MouseGetPosY();

	CGame* TheGame = CGame::GetInstance();
	
	for(int x = 0; x < TEXTBOXITEMS; x++)
	{
		if(PtInRect(&m_MenuRect[x], Clicker))
		{
			m_nSelectedItem = x;
		}
	}
}
